package org.zp.handler;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.midi.MidiDevice.Info;

import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.zp.dto.LoginDto;
import org.zp.dto.UserEdit;
import org.zp.pojo.AddressInfo;
import org.zp.pojo.UserInfo;
import org.zp.service.AddressService;
import org.zp.service.UserService;
import org.zp.service.UserServiceimpl;
import org.zp.util.Ajax;
import org.zp.util.Security;
import org.zp.util.ValiAdmindate;
import org.zp.util.Validate;
import org.zp.vo.FindAddressVo;

@RestController
public class UserHandler {
@Resource
private UserService u;
@Resource
private AddressService a;

//设置全局变量上传头像路径
String url = " ";
/**
 * 用户登录
 * @param name
 * @param pwd
 * @param code
 * @param req
 * @param resp
 * @param session
 */
@PostMapping("dologin.act")
public void userLogin(LoginDto dto,
		HttpServletRequest req, HttpServletResponse resp,HttpSession session) {
	Validate validate = new Validate();
	String account = dto.getName();
	String pwd = dto.getPwd();
	String code = dto.getCode();
	if(!validate.checkUserName(account)) {
		Ajax.error(resp, validate.getError(), 10000);
		return;
	}
	//验证码判断
	String sessonCode = (String) req.getSession().getAttribute("captcha");
	if(null == sessonCode|| null==code || !sessonCode.equalsIgnoreCase(code)) {
		Ajax.error(resp, "验证码错误", 10001);
		return;
	}
	Security md5 = new Security();
	String md5pwd = md5.encrypt16(pwd);
	UserInfo user = u.mybatisDoLogin(account,md5pwd);
	
			//登录失败
			if(user==null) {
			Ajax.error(resp, "用户名或密码错误", 1000);
			return;
			}
			//响应数据
			Ajax.success(resp,"date","usermain");
			req.getSession().setAttribute("user", user);
}
/**
 * 头像上传
 * @param file
 * @param req
 * @param resp
 * @return
 * @throws IOException
 */
@PostMapping("upload.act")
public String upload(MultipartFile file,HttpServletRequest req, HttpServletResponse resp) throws IOException {
	String path="D://";	
	System.out.println("预存路径为："+path);
	
	System.out.println(file.getName());// file - 变量的属性名
	System.out.println("文件名:"+file.getOriginalFilename());// 上传的文件名称

	//获取路径
	url =file.getOriginalFilename();
	System.out.println("文件名:"+url);
	
	//通过io流保存数据
	FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + file.getOriginalFilename()));
	return "success";
}
/**
 * 用户注册
 * @param uName
 * @param uAccount
 * @param password
 * @param checkPassword
 * @param sex
 * @param phone
 * @param req
 * @param resp
 */
@GetMapping("userReg.act")
public void reg(String uName,String uAccount,String password,String checkPassword,
		String sex,String phone,
		HttpServletRequest req, HttpServletResponse resp) {
	ValiAdmindate v = new ValiAdmindate();
	Validate va = new Validate();
	if(!va.checkUserName(uName)) {
		Ajax.error(resp, va.getError(), 10000);
		return;
	}
	if(!v.checkContent(uAccount)) {
		Ajax.error(resp, v.getError(), 10000);
		return;
	}
	if(!va.checkPassword(password)) {
		Ajax.error(resp, va.getError(), 10000);
		return;
	}
	if(!password.equals(checkPassword)) {
		Ajax.error(resp, "密码不相同", 10002);
		return;
	}
	int iSex = 0;
	try {
		iSex = Integer.parseInt(sex);
	} catch (NumberFormatException e) {
		Ajax.error(resp, "性别没选择", 10003);
		return;
	}
	
	if(!va.checkTell(phone)) {
		Ajax.error(resp, va.getError(), 10000);
		return;
	}
	
	Security md5 = new Security();
	String md5pwd = md5.encrypt16(password);
	UserInfo us = new UserInfo();
	us.setuName(uName);
	us.setuAccount(uAccount);
	us.setuPwd(md5pwd);
	us.setuSex(iSex);
	us.setTell(phone);
	us.setHead(url);
	
	//检查当前账号是否重复
	
	String count = us.getuAccount();
	String pwd = us.getuPwd();
	
	try {
		UserInfo user = u.mybatisDoLogin(count,pwd);
		if (!user.getuAccount().equals("")||user.getuAccount()!=null) {
			Ajax.error(resp, "注册失败已存在相同账号", 10001);
			return;
		}
	} catch (Exception e) {
		Integer x = u.mybatisAddUser(us);
		if (x>0) {
			Ajax.success(resp,"date","login");
			System.out.println("注册成功");
		}else {
			Ajax.error(resp, "注册失败", 10001);
			System.out.println("注册失败");
		}
	}
	
	
}
/**
 * 欢迎xx
 * @param req
 * @param resp
 */
@GetMapping("getUserName.act")
public void getUserName(HttpServletRequest req, HttpServletResponse resp) {
	UserInfo user = (UserInfo) req.getSession().getAttribute("user");
	String username = user.getuName();
	Integer uid = user.getuId();
	UserInfo use = u.getUserByuid(uid);
	System.out.println("欢迎"+username+"  id"+uid);
	Ajax.success(resp, "user", use);
	
}
/**
 * 修改用户信息
 * @param dto
 * @param req
 * @param resp
 */
@PostMapping("userEdit.act")
public void userEdit(UserEdit dto, HttpServletRequest req, HttpServletResponse resp) {
Integer uid =Integer.parseInt(dto.getUid());
String uName = dto.getuName();
String uTell = dto.getuTell();
String cPwd = dto.getcPwd();
String uPwd = dto.getuPwd();
String urll =dto.getHead(); 
System.out.println(uid);
System.out.println(uName);
System.out.println(uTell);
System.out.println(cPwd);
System.out.println(uPwd);
System.out.println(urll);
Security md5 = new Security();
String md5pwd = md5.encrypt16(uPwd);
UserInfo us = new UserInfo();
us.setuId(uid);
us.setuName(uName);
us.setuPwd(md5pwd);
us.setTell(uTell);
//判断其他值是否为空
ValiAdmindate validate = new ValiAdmindate();
if(!validate.checkUserName(uName)) {
	Ajax.error(resp, validate.getError(), 10000);
	return;
}
if(!validate.checkContent(uTell)) {
	Ajax.error(resp, validate.getError(), 10000);
	return;
}
if(!validate.checkContent(cPwd)) {
	Ajax.error(resp, validate.getError(), 10000);
	return;
}
if(!validate.checkContent(uPwd)) {
	Ajax.error(resp, validate.getError(), 10000);
	return;
}
if(!uPwd.equals(cPwd)) {
	Ajax.error(resp, "密码不相同", 10002);
	return;
}

//判断上传的图片是否是空的
if (url.equals(" ")) {
	us.setHead(urll);
}else {
	us.setHead(url);
}

Integer index = u.updateUserInfo(us);
if (index>0) {
	Ajax.success(resp);
	System.out.println("修改成功");
	return;
}else {
	Ajax.error(resp, "修改失败",10001);
	System.out.println("修改失败");
}

}
/**
 * 充值
 * @param uid
 * @param money
 * @param req
 * @param resp
 */
@PostMapping("useraddMoney.act")
public void useraddMoney(String uid,String money,
		HttpServletRequest req, HttpServletResponse resp) {
	Integer Uid = 0;
	Integer Money = 0;
	try {
		 Uid = Integer.parseInt(uid);
		 Money = Integer.parseInt(money);
	} catch (Exception e) {
		Ajax.error(resp, "错误格式");
		return;
	}
	//查询用户当前的余额
	UserInfo use = u.getUserByuid(Uid);
	System.out.println("充值前余额："+use.getuMoney());
	 
	Integer oldMoney = use.getuMoney();
	Integer newMoney = oldMoney+Money;
	
	Integer index = u.useraddMoney(Uid,newMoney);
	if (index>0) {
		
		Ajax.success(resp,"newMoney",newMoney);
		System.out.println("充值成功");
		return;
	}else {
		Ajax.error(resp, "充值失败",10001);
		System.out.println("充值失败");
	}
}
/**
 * 获得当前用户所在网点
 * @param area
 * @param city
 * @param req
 * @param resp
 */
@GetMapping("findAddress.act")
public void findAddress(String area,String city,HttpServletRequest req, HttpServletResponse resp) {
	UserInfo user = (UserInfo) req.getSession().getAttribute("user");
	Integer uid = user.getuId();
	Integer areaid = Integer.parseInt(area);
	Integer cityid = Integer.parseInt(city);
	FindAddressVo addressinfo = a.findAddress(uid,areaid,cityid);
	
	Ajax.success(resp, "addressinfo", addressinfo);
}

@GetMapping("getaddressln.act")
public void getaddressln(String one,String tow,String three,HttpServletRequest req, HttpServletResponse resp) {
	Integer one1 = 0;
	Integer tow1 = 0;
	Integer three1 = 0;
	try {
		one1 = Integer.parseInt(one);
		tow1 = Integer.parseInt(tow);
		three1 = Integer.parseInt(three);
	} catch (Exception e) {
		System.out.println("寄件值或空或转换失败");
	}
	
	if (one1==0||tow1==0||three1==0) {
		Ajax.error(resp, "请补充完整要搜索的网点信息");
		return;
	}
	
	AddressInfo addressln = a.getaddressln(three1);
    Ajax.success(resp, "addressln", addressln);
	
}


}
